package com.neutech.dao.impl;

import com.neutech.dao.businessDAO;
import com.neutech.entity.admin;
import com.neutech.entity.business;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class businessAchieve implements businessDAO {
    Connection connection = databaseConnection.getConnection();

    static PreparedStatement ps = null;
    static ResultSet rs = null;
    static business business = new business();

    @Override
    public List<com.neutech.entity.business> listBusiness(){
        List<business> list = new ArrayList<>();
        try{
            ps = connection.prepareStatement("select * from business");
            rs = ps.executeQuery();
            while (rs.next()){
                business.setBusinessId(rs.getInt("businessId"));
                business.setPassword(rs.getString(("password")));
                business.setBusinessName(rs.getString(("businessName")));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                System.out.println(business);
                list.add(business);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public int getBusinessByIdByPass(Integer Id, String Pass) {
        int count = 0;
             try {
                 ps = connection.prepareStatement("select * from business where businessId = ? and password = ?");

                 ps.setInt(1,Id);
                 ps.setString(2,Pass);

                 rs = ps.executeQuery();
                 if(rs.next()){
                     count++;
                 }
             } catch (SQLException e) {
                 e.printStackTrace();
             }
             return count;
    }

    @Override
    public int registerBusiness(Integer Id, String Pass) {
        int count = 0;
        try{
            ps = connection.prepareStatement("insert into business(businessId,password) values (?,?)");
            ps.setInt(1,Id);
            ps.setString(2,Pass);

            ps.executeUpdate();
            count++;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return count;
    }

    @Override
    public int getBusinessById(Integer Id) {
        int count = 0;
        try{
            ps = connection.prepareStatement("select * from business where businessId = ?");
            ps.setInt(1,Id);

            rs = ps.executeQuery();
            while (rs.next()){
                business.setBusinessId(rs.getInt("businessId"));
                business.setPassword(rs.getString(("password")));
                business.setBusinessName(rs.getString(("businessName")));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                System.out.println(business);
                count++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int getBusinessByName(String Name) {
        int count = 0;
        try{
            ps = connection.prepareStatement("select * from business where businessName = ?");
            ps.setString(1,Name);

            rs = ps.executeQuery();
            while (rs.next()){
                business.setBusinessId(rs.getInt("businessId"));
                business.setPassword(rs.getString(("password")));
                business.setBusinessName(rs.getString(("businessName")));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                System.out.println(business);
                count++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int addBusiness(business business) {
        int count = 0;
        try{
            ps = connection.prepareStatement("insert into business(businessId,password,businessName,businessAddress,businessExplain,starPrice,deliveryPrice) values (?,?,?,?,?,?,?)");
                ps.setInt(1,business.getBusinessId());
                ps.setString(2,business.getPassword());
                ps.setString(3,business.getBusinessName());
                ps.setString(4,business.getBusinessAddress());
                ps.setString(5,business.getBusinessExplain());
                ps.setDouble(6,business.getStarPrice());
                ps.setDouble(7,business.getDeliveryPrice());

                ps.executeUpdate();
                count++;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        return count;
        }

    @Override
    public int updateBusiness(business business) {
        int count = 0;
        try{
            ps = connection.prepareStatement("update business set businessId = ?,businessName = ?,businessAddress = ?,businessExplain = ?,starPrice = ?,deliveryPrice = ? where businessName = ?");
            ps.setInt(1,business.getBusinessId());
            ps.setString(2,business.getBusinessName());
            ps.setString(3,business.getBusinessAddress());
            ps.setString(4,business.getBusinessExplain());
            ps.setDouble(5,business.getStarPrice());
            ps.setDouble(6,business.getDeliveryPrice());
            ps.setString(7,business.getBusinessName());

            ps.executeUpdate();
            count++;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int deleteBusiness(String Name) {
        int count = 0;
        try{
            ps = connection.prepareStatement("delete from business where businessName = ?");
            ps.setString(1,Name);

            ps.executeUpdate();
            count++;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int updatePassword(Integer Id, String Pass) {
        int count = 0;
        try {
            ps = connection.prepareStatement("update business set password = ? where businessName = ?");
            ps.setString(1, Pass);
            ps.setInt(2, Id);

            count++;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
